package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.CvResume;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.params.EmailParams;
import cn.com.hhrcw.service.ICvResumeService;
import cn.com.hhrcw.service.IFileService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.sms.service.ISysSmsTemplateService;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: recruit-boot-parent
 * @description: 发邮件
 * @author: JD
 * @create: 2020-11-24 15:54
 **/
@RestController
@Slf4j
@RequestMapping("/admin/email")
public class EmailController {

    @Autowired
    private ISysSmsTemplateService service;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private ICvResumeService resumeService;

    @Autowired
    IFileService fileService;


    /**
     * 发送邮件携带简历
     *
     * @param params
     * @return
     */
    @PostMapping("/send")
    public Result sendEmail(@RequestBody EmailParams params) {
        List<PiInfo> piInfoList = piInfoService.lambdaQuery().eq(PiInfo::getDelFlag, false).in(PiInfo::getUserId, params.getReUserIds()).list();
        if (!CollectionUtils.isEmpty(piInfoList)) {
            List<String> emails = new ArrayList<>();
            piInfoList.forEach(e -> {
                if (StringUtils.isNotBlank(e.getEmail())) {
                    emails.add(e.getEmail());
                }
            });
            if (!CollectionUtils.isEmpty(emails)) {
                String[] emailArray = emails.toArray(new String[emails.size()]);
                service.sendEmail(params.getText(), params.getSubject(), emailArray);
            }
        }
        return Result.ok();
    }

    /**
     * 发送邮件,带简历
     *
     * @param params
     * @return
     */
    @PostMapping("/sendWithFile")
    public Result sendWithFile(@RequestBody EmailParams params) {
        List<PiInfo> piInfoList = piInfoService.lambdaQuery().eq(PiInfo::getDelFlag, false).in(PiInfo::getUserId, params.getReUserIds()).list();
        if (!CollectionUtils.isEmpty(piInfoList)) {
            List<String> emails = new ArrayList<>();
            piInfoList.forEach(e -> {
                if (StringUtils.isNotBlank(e.getEmail())) {
                    emails.add(e.getEmail());
                }
            });
            if (!CollectionUtils.isEmpty(emails)) {
                String[] emailArray = emails.toArray(new String[emails.size()]);
                CvResume resume = resumeService.getById(params.getReId());
                String url = "";
                String html = "";
                File emailFile = null;
                if (resume.getType() == 0) {
                    //在线
                    if (resume.getLanType() == 0) {
                        //中文
                        url = org.apache.commons.lang.StringUtils.isNotBlank(resume.getPdfPath()) && StringUtils.isBlank(params.getJobId()) ? resume.getPdfPath() : resumeService.getResumeInfoPDF(resume.getUserId(), 0,params.getJobId());
                    } else {
                        url = org.apache.commons.lang.StringUtils.isNotBlank(resume.getPdfPathEn()) ? resume.getPdfPathEn() : resumeService.getResumeInfoPDF(resume.getUserId(), 1,params.getJobId());
                    }
                    html = params.getText() + "<br />" + FileUtil.readString(url, StandardCharsets.UTF_8);
                } else {
                    cn.com.hhrcw.entity.File file = fileService.getById(resume.getFileId());
                    emailFile = new File(file.getFilePath());
                    html = params.getText();
                }
                service.sendEmail(html, params.getSubject(), emailFile, emailArray);
            }
        }
        return Result.ok();
    }
}
